package com.example.logindemo.service.impl;

import com.example.logindemo.entity.ReviewImage;
import com.example.logindemo.repository.ReviewImageRepository;
import com.example.logindemo.service.ReviewImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class ReviewImageServiceImpl implements ReviewImageService {

    @Autowired
    private ReviewImageRepository reviewImageRepository;

    @Override
    @Transactional(readOnly = true)
    public Optional<ReviewImage> getReviewImageById(Integer id) {
        return reviewImageRepository.findById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public List<ReviewImage> getImagesByReviewId(Integer reviewId) {
        return reviewImageRepository.findByReviewId(reviewId);
    }

    @Override
    @Transactional
    public ReviewImage saveReviewImage(ReviewImage reviewImage) {
        return reviewImageRepository.save(reviewImage);
    }

    @Override
    @Transactional
    public List<ReviewImage> saveAllReviewImages(List<ReviewImage> reviewImages) {
        return reviewImageRepository.saveAll(reviewImages);
    }

    @Override
    @Transactional
    public void deleteReviewImage(Integer id) {
        reviewImageRepository.deleteById(id);
    }

    @Override
    @Transactional
    public void deleteImagesByReviewId(Integer reviewId) {
        reviewImageRepository.deleteByReviewId(reviewId);
    }

    @Override
    @Transactional(readOnly = true)
    public long countImagesByReviewId(Integer reviewId) {
        return reviewImageRepository.countByReviewId(reviewId);
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<ReviewImage> findByReviewIdAndFileName(Integer reviewId, String fileName) {
        ReviewImage reviewImage = reviewImageRepository.findByReviewIdAndFileName(reviewId, fileName);
        return Optional.ofNullable(reviewImage);
    }
    

}